// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

// 引入es6转码
import "babel-polyfill";
import Vue from "vue";

// 引入element-ui
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

import App from "./App";
import router from "./router";
import store from "./store";
import Vuex from "vuex";

// 引入lodash
import _ from "lodash";
// 扩展vue的插件
Vue.prototype._ = _;

// 引入axios
import Axios from "axios";
// 引入nprogress
import NProgress from "nprogress";
import "nprogress/nprogress.css";

// 引入全局接口配置文件
import FdConfig from "./assets/js/config";
// 引入全局共用方法
import FdGlobal from "./assets/js/common/global";

// 引入暂无数据判断的盒子
import emptyBox from "./components/emptyBox";
Vue.component("emptyBox", emptyBox);

// 引入vue-jsonp
// import * as vueJsonp from "vue-jsonp";
// Vue.use(vueJsonp);
// console.log(vueJsonp.install);

Vue.use(Vuex);

Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.prototype.$axios = Axios;
Axios.defaults.withCredentials = true;

Vue.prototype.$config = new FdConfig();
Vue.prototype.$global = new FdGlobal();

// 请求进度条配置
NProgress.configure({
  easing: "ease", // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
});

const axiosPromiseArr = [];
// 设置axios超时时间
Axios.defaults.timeout = 30000;
// Axios请求拦截
Axios.interceptors.request.use((config) => {
  const now = (new Date()).getTime();
  // 请求开始，蓝色过渡滚动条开始出现
  NProgress.start();
  if (config.url) {
    if (config.url.indexOf("?") < 0) {
      config.url += `?t=${now}`;
    } else {
      config.url += `&t=${now}`;
    }
  }

  // 在发送请求设置cancel token
  config.cancelToken = new Axios.CancelToken((cancel) => {
    axiosPromiseArr.push({ cancel });
  });
  return config;
}, (error) => {
  // Do something with request error
  Vue.prototype.$message.error("出错了!");
  return Promise.reject(error);
});
// 处理路由切换时没有请求完成的接口
router.beforeEach((to, from, next) => {
  axiosPromiseArr.forEach((ele, index) => {
    ele.cancel();
    delete axiosPromiseArr[index];
  });
  next();
});
// 全局的AXios返回出错请求处理
Axios.interceptors.response.use((response) => {
  // todo 后端返回错误码处理错误状态
  // 如果用户登录超时
  if (response.data.code === 401) {
    Vue.prototype.$message({
      type: "error",
      offset: 10,
      duration: 1000,
      message: response.data.message
    });
    if (router.history.current.path !== "/login") {
      // 取消后面的请求
      // source.cancel();
      axiosPromiseArr.forEach((ele, index) => {
        ele.cancel();
        // source.cancel(ele);
        delete axiosPromiseArr[index];
      });
      router.replace({
        path: "/login"
      });
    }
    window.sessionStorage.clear();
    NProgress.done();
    return Promise.reject(response.data.message);
  }
  NProgress.done();
  return response.data;
}, (error) => {
  NProgress.done();
  if (Axios.isCancel(error)) {
    //
    return new Promise(() => {});
  } else {
    return Promise.reject(error);
  }
});
/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  store,
  components: { App },
  template: "<App/>"
});
